// component/uploadImage/uploadImage.js
import ajax from '../../utils/index'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    text: {
      type: String,
      value: '拍照'
    },
    status: String,
    type: {
      type: String,
      value: 'image'
    },
    value: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    filePath: null
  },

  /**
   * 组件的方法列表
   */
  methods: {
    deleteEvent() {
      const type = this.data.type
      wx.showModal({
        title: '提示',
        content: `确定删除该${type == 'video' ? '视频' : '图片'}吗?`,
        success: (res) => {
          if (!res.confirm) return
          this.setData({ filePath: null })
          this.triggerEvent("change", null)
        }
      })
    },
    async uploadEvent() {
      const type = this.data.type
      const filePath = await ajax.chooseImage(type)
      const url = type == 'video' ? '/common/uploadFile2Cos' : '/wx/appletFeedback/uploadimg'
      const fileRes = await ajax.uploadFile({ filePath }, url);
      this.setData({ filePath })
      this.triggerEvent("change", fileRes.url || fileRes.data.url)
    },
  }
})
